package presentation;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;

import javax.swing.WindowConstants;

import data.Dictionary;
import data.DictionaryHolder;

import util.Utils;

public class DistributionDisplayer {

	public static Logger logger = Logger.getLogger(DictionaryHolder.class.getName());

	public static List<CustomizedChart> chartsHolder = new ArrayList<CustomizedChart>();

	public static void showDistribution(Dictionary dict) {

		List<Integer> uniqueHashes = Utils.getUniqueValues(dict.getHashes());
		Collections.sort(uniqueHashes);

		logger.info("\n************************************" + "\n" + "Hash function name: " + dict.getName() + "\n" + "Total words number:"
				+ dict.getWords().size() + "\n" + "Unique hashes number:"
				+ uniqueHashes.size() +
				"\n************************************" + "\n");

	}

	public static void showDistributions(DictionaryHolder dictionaries) {

		for (Dictionary dict : dictionaries.getAllDictionaries()) {

			showDistribution(dict);
			
			List<Point> histogram = generateHistogram(dict.getHashes());
			
			CustomizedChart chartResultLinePanel = new CustomizedChart(dict.getName(), histogram.size()/10 , (int)getMax(histogram)/6, 0, 0);

			chartResultLinePanel.addBars(histogram, dict.getName()+" distribution", Color.BLUE);
			chartResultLinePanel.pack();
			chartResultLinePanel.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			chartsHolder.add(chartResultLinePanel);
		}

		for (CustomizedChart chart : chartsHolder) {
			chart.setVisible(true);
		}
	}

	static List<Point> generateHistogram(List<Integer> hashes) {

		List<Point> points = new ArrayList<Point>();
		int max = Collections.max(hashes);

		int[] histogram = new int[max + 1];

		for (Integer integer : hashes) {
			histogram[integer]++;
		}

		for (int i = 0; i < histogram.length; i++) {
			points.add(new Point(i, histogram[i]));
		}

		return points;
	}
	
	static int getMax(List<Point> points){
		
		int max = 0;
	for (Point point : points) {
		if(point.y>max) max = (int)point.y;
	}	
	return max;	
	}
}